import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [], // 轮播图数据
    recommendList: [], // 推荐歌单
    topList: [], // 排行榜数据
    playList:[], //热榜排行

    token: '',  //登陆成功的token
    musicIds:[], //喜欢的音乐列表
    userInfo:[], //用户登录后的信息
    likeSongList:[], //喜欢的音乐信息
    musicUrl:[], //当前播放的歌曲的url地址
  },

  
  /**
   * 生命周期函数--监听页面加载
   * async 异步
   */
  onLoad: async function (options) {
    //发送请求，获取到动态的banner以及热榜
    let bannerListData = await request('/banner', {type: 2});
    let playListData = await request('/top/playlist/highquality', {type: 2}); 

    //将接受到的数据存到data中去
    this.setData({
        token:wx.getStorageSync('token'),
        userInfo:wx.getStorageSync('userInfo'),
        bannerList: bannerListData.banners,
        playList: playListData.playlists
       })
    let userId = this.data.userInfo.userId
    // console.log("uid是："+ this.data.userInfo.userId)
    //根据获得的信息，继续请求数据，这里请求的是当前用户喜欢的列表
    let musicList = await request('/user/playlist', {uid: this.data.userInfo.userId});
    
    this.setData({
        musicIds: musicList.playlist
    })

    //console.log(this.data.musicIds[0].id)

    //根据用户喜欢的列表，来请求到歌曲
    let favoriteList = await request('/playlist/detail', {id: this.data.musicIds[0].id});
    // console.log(this.data.favoriteList)
    this.setData({
      likeSongList: favoriteList.playlist.tracks
    })
   
    
    // 获取推荐歌单数据
    let recommendListData = await request('/personalized', {limit: 10});
    this.setData({
      recommendList: recommendListData.result
    })
    
    this.loadMusicUrl();

    //设置初始歌曲
    this.setData({
      nowposter: this.data.likeSongList[0].al.picUrl,
      name: this.data.likeSongList[0].name,
      author: this.data.likeSongList[0].ar[0].name,
      src: this.data.musicUrl,
    })
  },
  
  // 跳转至recommendSong页面的回调
  toRecommendSong(){
    wx.navigateTo({
      url: '/songPackage/pages/recommendSong/recommendSong'
    })
  },
  // 跳转至私人fm页面
  topersonalFM(){
    wx.navigateTo({
      url: '/songPackage/pages/fm/fm'
    })
  },

  //跳转至排行榜
  toRankMusic(){
    wx.navigateTo({
      url: '/songPackage/pages/rank/rank'
    })
  },

  //跳转至电台
  toRadio(){
    wx.navigateTo({
      url: '/songPackage/pages/radio/radio'
    })
  },

  //跳转至直播
  toLive(){
    wx.navigateTo({
      url: '/songPackage/pages/live/live'
    })
  },
  
  
  //mp3播放器
    audioPlay: function () {
      this.audioCtx.play()
    },
    audioPause: function () {
      this.audioCtx.pause()
    },
    audio14: function () {
      this.audioCtx.seek(60)
    },
    audioStart: function () {
      this.audioCtx.seek(0)
    },
  
    //加载歌曲的url
    loadMusicUrl: async function(i=0){
      let musicUrlData = await request('/song/url', {id: this.data.likeSongList[i].id});
      console.log(this.data.likeSongList[i].name)
      this.setData({
      musicUrl:musicUrlData.data[0].url
      })
      this.setData({
        nowposter: this.data.likeSongList[i].al.picUrl,
        name: this.data.likeSongList[i].name,
        author: this.data.likeSongList[i].ar[0].name,
        src: this.data.musicUrl,
      });
    },

    playMusic(e){
      let i = e.currentTarget.id
      this.loadMusicUrl(i);
      this.audioPlay();
    },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio')  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
